	 											
package com.module.device.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import base.dao.GenericHibernateDao;
import base.util.Page;
import com.module.device.vo.DeviceLifecycle;

public class DeviceLifecycleDao  extends GenericHibernateDao<DeviceLifecycle, Long> {	
	private DeviceLifecycleDao(){}
	
	private static DeviceLifecycleDao dao;
	
	public synchronized static DeviceLifecycleDao getInstance(){
		if(dao==null)
			dao = new DeviceLifecycleDao();
		return dao;
	}
	/**
	 * 查询列表
	 * @param session
	 * @param bean
	 * @param start
	 * @param limit
	 * @return
	 * @throws SQLException
	 */
	public Page getList(Session session,DeviceLifecycle bean,Integer start, Integer limit)throws SQLException{
		Page page = new Page();
		StringBuffer sb = new StringBuffer();
	    StringBuffer condition = new StringBuffer();
	    String deviceTypeName="";
		if(bean!=null)
		{
			deviceTypeName=bean.getSearchDeviceTypeName();
		}
		sb.append("from DeviceLifecycle as obj where 1=1 and obj.status<>'D' and (obj.lifecycleStatus='A' or obj.lifecycleStatus='S' or obj.lifecycleStatus='E')");
		if(!"".equals(deviceTypeName)&&!(deviceTypeName==null))
		{
			condition.append( " and obj.deviceEntity.deviceType.deviceTypeName='"+deviceTypeName+"'");
			sb.append(condition);
		}
		System.out.println("`````````````SQL:"+sb);
		Query query = session.createQuery(sb.toString());
		query.setFirstResult(start).setMaxResults(limit);
		Query totalQuery = session
		.createQuery("select count(*) from DeviceLifecycle as obj where 1=1 and obj.status<>'D' and (obj.lifecycleStatus='A' or obj.lifecycleStatus='S' or obj.lifecycleStatus='E')"+condition);
		page.setTotal((long) ((Long) totalQuery.uniqueResult()).intValue());
		page.setRows(query.list());	
		return page;
	}
	
	
	
	/**
	 * 查询设备名称列表
	 * @param session
	 * @param bean
	 * @param start
	 * @param limit
	 * @return
	 * @throws SQLException
	 */
	public Page findByDeviceTypeName(Session session,String deviceTypeName,Integer start, Integer limit)throws SQLException{
		Page page = new Page();
		StringBuffer sb = new StringBuffer();
	    StringBuffer condition = new StringBuffer();
		List<Object> params = new ArrayList<Object>();
		sb.append("from DeviceLifecycle as obj where 1=1 and obj.deviceEntity.deviceType.deviceTypeName='"+deviceTypeName+"'");
		sb.append(condition);
		Query query = session.createQuery(sb.toString());
		query.setFirstResult(start).setMaxResults(limit);
		Query totalQuery = session
		.createQuery("select count(*) from DeviceLifecycle as obj where 1=1 and obj.deviceEntity.deviceType.deviceTypeName='"+deviceTypeName+"'");
		page.setTotal((long) ((Long) totalQuery.uniqueResult()).intValue());
		page.setRows(query.list());	
		return page;
	}
	
	
	
	
	
}

